<?php
/**
* Banklink base
* 
* Currently supported banks:
* <ul>
* <li> Swedbank </li>
* <li> SEB </li>
* <li> Sampopank </li>
* <li> Nordea </li>
* <li> EstCard krediitkaart </li>
* <li> IPX - Only for wap payments! </li>
* <li> Dummy - test bank that basicly accepts all transfers. </li>
* </ul>
* 
* @author Oliver Leisalu
* @package BankLink
*/
abstract class BankLink {
	
	/**
	 * If banklink is usable. Defined in conf file.
	 * @var Bool
	 */
	protected $usable = false;
	/**
	* Url to where user is sent back from bank
	* @var String
	*/
	protected $callbackUrl;
	/**
	* Server to where request is sent.
	* @var String
	*/
	protected $serverUrl;
	/**
	* Data sent to bank encoding.
	* @var String
	*/
	protected $outputEncoding = 'ISO-8859-4';
	
	/**
	* Data sent to bank encoding.
	* @var String
	*/
	protected $inputEncoding = 'UTF-8';
	/**
	* Config file where all banks data is held.
	* @var String
	*/
	protected $configFilePath;


	public function __construct() {
		$this->configFilePath = self::getConfFilePath();
		
		if( !file_exists( $this->configFilePath ) )
			trigger_error('Config file is not found. Expected path: <strong>'.$this->configFilePath.'</strong>', E_USER_ERROR );
			
		require( $this->configFilePath );
		
		$key = substr( get_class($this), 0, -4 );
		if( !isset($BANKLINK[$key]) )
			trigger_error('Banklink configuration for '.$key.' doesn\'t exist!', E_USER_ERROR);
			
		foreach( $BANKLINK[$key] as $key => $value  ) 
			$this->$key = $value;

	}
	
	/**
	 * @return String
	 */
	final public static function getConfFilePath() {
		return DOM_CONF_DIR_PATH . 'banklink.conf.php';
	}
	
	/**
	 * @param String $var
	 * @return Mixed
	 */
	final public function __get( $var ) {
		return $this->$var;
	}
	
	/**
	* @param String [hansa|seb|nordea|sampo|estcard]
	* @param String $configFilePath
	* @return BankLink
	*/
	final public static function getLink( $link, $configFilePath = NULL ) {
		switch( strtolower($link ) ) {
			case 'swed': 
				return new SwedLink( $configFilePath );
			case 'seb': 
				return new SebLink( $configFilePath );
			case 'nordea': 
				return new NordeaLink( $configFilePath );
			case 'sampo': 
				return new SampoLink( $configFilePath );
			case 'estcard': 
				return new EstCardLink( $configFilePath );
			case 'ipx': 
				return new IpxLink( $configFilePath );
			case 'dummy': 
				return new DummyLink( $configFilePath );
			default:
				trigger_error('Unknown link '.$link.'', E_USER_ERROR);
			break;
		}
	}
	
	/**
	 * Determines bank and handles callback
	 * Array parts:
	 * <ul>
	 * <li>status - 0-failed, 1-ok, 3-user cancelled, 4-invalid signature.</li>
	 * <li>logId</li>
	 * <li>bankError</li>
	 * <li>bankErrorStr</li>
	 * </ul>
	 * @return Array
	 */
	final public static function handleReplay() {
		
		if( $_REQUEST['VK_SND_ID'] == 'SAMPOPANK') {
			$link = new SampoLink(); 
			
		} else if( $_REQUEST['VK_SND_ID'] == 'EYP') {
			$link = new SebLink(); 
		
		} else if( $_REQUEST['VK_SND_ID'] == 'HP') {
			$link = new SwedLink();
		
		} else if( isset($_REQUEST['SOLOPMT_RETURN_VERSION']) ) {// Currently SOLOPMT_ is unique for Nordea
			$link = new NordeaLink(); 
		
		} else if( isset($_REQUEST['action']) && isset($_REQUEST['ver']) &&  isset($_REQUEST['ecuno']) && isset($_REQUEST['receipt_no']) ) {
			$link = new EstCardLink();
		
		} else if( $_REQUEST['BankLinkBank'] === 'Ipx' ) {
			$link = new IpxLink(); 
		
		} else if( $_REQUEST['fromDummy'] === 'dummy' ) {
			$link = new DummyLink(); 

		}  else {
			
			return array('status' => 0, 'logId' => 0, 'bankError'=>'', 'bankErrorStr'=>'Can\'t resolve bank!');
			//throw new RuntimeException('Could not resolve bank');
		}
		
		return $link->handleCallback();
	}
	
	/**
	 * Send user to bank. This method must change header or show html page that directs user to bank.
	 *
	 * @param Int $callBackId - send this as transfer id. It is always unique and is needed to handle callback
	 * @param BankPayment $payment
	 */
	abstract public function sendToBank( $callBackId, BankPayment $payment );
	
	/**
	 * Returns HTML form that directs user to $link->serverUrl.
	 * Has submit button and javascript timer.
	 *
	 * @param BankLink $link
	 * @param String $fields html form fields.
	 * @return String
	 */
	final public static function getFormBase( BankLink $link, $fields ) {
		return '
			<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
			<html xmlns="http://www.w3.org/1999/xhtml">
			<head>
			<meta http-equiv="Content-Type" content="text/html; charset='.$link->outputEncoding.'" />
			<title>Suunamine</title>
			</head>
			<body>
			<form id="form" action="'.$link->serverUrl.'" method="POST">
				'.$fields.'
				<input type="submit" value="Edasi panka"/>
			</form>
			<script type="text/javascript"> t = setTimeout( "document.getElementById(\"form\").submit()" ,500 ); </script>	
			</body>
			</html>';
	}
	/**
	 * Enter description here...	 
	 * Function that processes the callback from the bank
	 *
	 * Statuses: See BankPaymentReplay::$status
	 * @return Array [status=>Int, logId=>Int, bankError=>String, bankErrorStr=>String]
	 *
	 */
	abstract protected function handleCallback();
}
?>